using System.Collections.Generic;
using UnityEngine;

namespace Gj
{
    public class CacheService : GameService
    {
        public static CacheService single;

        private Dictionary<string, List<GameObject>> cache = new();

        static CacheService()
        {
            single = new CacheService();
        }

        public void OnDestroy()
        {
            single = new CacheService();
        }

        public void OnInit()
        {
            cache = new Dictionary<string, List<GameObject>>();
        }

        public void OnUpdate()
        {
            
        }

        public GameObject GetCache(string key)
        {
            if (cache != null && cache.ContainsKey(key))
            {
                var list = cache[key];
                GameObject obj = null;
                if (list.Count > 0)
                {
                    obj = list[0];
                    list.RemoveAt(0);
                }
                if (obj != null) obj.SetActive(true);
                return obj;
            }

            return null;
        }

        public void SetCache(string key, GameObject obj)
        {
            if (!cache.ContainsKey(key)) cache.Add(key, new List<GameObject>());
            cache[key].Add(obj);
        }
    }
}